package com.atu.servlet;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class HelloServlet implements Servlet {
    public HelloServlet() {
        System.out.println("这是构造器");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("这是初始化方法");
//        1、可以获取 Servlet 程序的别名 servlet-name 的值
        System.out.println("HelloServlet的程序名是："+servletConfig.getServletName());
//        2、获取初始化参数 init-param
        System.out.println("用户名："+servletConfig.getInitParameter("username"));
        System.out.println("用户密码："+servletConfig.getInitParameter("password"));
//        3、获取 ServletContext 对象
        System.out.println(servletConfig.getServletContext());
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    /**
     * service 方法是专门用来处理请求和响应的
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("servlet被访问了");
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String method = httpServletRequest.getMethod();
        //System.out.println(method);
        if ("GET".equals(method)) {
            System.out.println("get请求");
        }else if ("POST".equals(method)){
            System.out.println("post请求");

        }
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("这是销毁方法");
    }
}
